#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 30

int main(int argc, char *argv[])
{
    int sock;
    char message[BUF_SIZE];
    struct sockaddr_in my_addr, your_addr;
    socklen_t addr_size;
    size_t str_len;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family = PF_INET;
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    my_addr.sin_port = htons((uint16_t)atoi(argv[1]));

    bind(sock, (const struct sockaddr *)&my_addr, sizeof(my_addr));

    for(int i = 0;i < 3;++i) {
        sleep(2);
        addr_size = sizeof(your_addr);
        str_len = (size_t)recvfrom(sock, message, BUF_SIZE, 0, (struct sockaddr *)&your_addr, &addr_size);
        printf("消息长度: %d\n", (int)str_len);
        printf("消息 %d: %s \n", i + 1, message);
    }
    close(sock);
    return 0;
}
